package com.lgq.listener;


import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import com.lgq.dao.DaoFactory;
import com.lgq.dao.VisitNumDao;
import com.lgq.entity.VisitNum;

/**
 * @author 刘光强
 * @date 2012-3-26 下午3:25:52
 * @version 1.0
 * @监听器类，监听应用启动和关闭，执行相关的定时任务
 **/
public class ApplicationListener implements ServletContextListener {
	private Integer  visitNum=0;
	private Integer onlineNum=0;
	private VisitNumDao visitNumDao=DaoFactory.getVisitNumDao();
	/**
	 * 在Web应用启动时初始化任务
	 */
	public void contextInitialized(ServletContextEvent event) {
		System.out.println("启动全局监听");
		try {
			visitNum=visitNumDao.getVisitNum().getVisitnum();
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}
		event.getServletContext().setAttribute("visitnum", visitNum);
		event.getServletContext().setAttribute("onlinenum", onlineNum);
	}

	/**
	 * 在Web应用结束时停止任务
	 */
	public void contextDestroyed(ServletContextEvent event) {
		VisitNum visitNum=new VisitNum();
		visitNum.setId(1);
		visitNum.setVisitnum((Integer)event.getServletContext().getAttribute("visitnum"));
		visitNumDao.updateVisitNum(visitNum);
	}
}